/*
 * CPassiveThread.h
 *
 *  Created on: 2011-9-14
 *      Author: stargui
 */

#ifndef CPASSIVETHREAD_H_
#define CPASSIVETHREAD_H_
#include <map>
#include <string>
#include "CommonLibrary.h"
#include "../../SocketUtils/CPSocketUtils.h"
#include "../../Session/CSessionManager.h"


class CPassiveThread //public Common::CThread
{

public:

	CPassiveThread();

	virtual ~CPassiveThread();

//private:

	virtual void *ThreadFuncImpl();

public:

	long GetCurrentLoads();

	void OnSocketConnect(int handle);

	void SocketInspectTimeout();

	void SetReleaseSockets();

	void SetClientNo(unsigned int no);

private:

	bool SocketRecvTcpStream(int handle);

	void ReleaseSockets();

	void SocketDisconnect(int handle);

	bool CertificateConnect(int handle, std::string &session, Json::Value &request);

	bool CertificateStream(int handle, std::string &session, Json::Value &request);

	bool CertificateKeepalive(int handle, std::string &session, Json::Value &request);

	bool CertificateDescribe(int handle, std::string &session, Json::Value &request);

	bool CertificateLogin(int handle, std::string &session, Json::Value &request);

	bool CertificateVerify(int handle, std::string &session, Json::Value &request);

	bool ProcessCallbackTask(int source,int taskhandle, Json::Value &response,int type,void *threadid);

	//chech status by level 0:session,1:task 2:all
	bool ProcessTask(std::string &session,int level,unsigned int taskindex);

private:

	char *_buffer;

	bool _release;

	unsigned int _clientNo;

	unsigned long _loads, _interval;

	CSessionManager *_sessionManager;

	struct _TcpSocket
	{
		bool slave;
		long interval;
		unsigned int	taskindex;
		std::string S0;
		std::string session;
		std::string buffer;
		unsigned char steps;
	};

	std::map<int, _TcpSocket> _socketContainer;

	std::map<std::string, boost::function<bool(int, std::string &, Json::Value &)> > _operation;

	CProtocolFactory _packet;
	Json::Value _request;
	Json::Reader _reader;
};

#endif /* CPASSIVETHREAD_H_ */
